package com.sangfor.ssl.service.auth;

import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.setting.SystemConfiguration;
import com.sangfor.ssl.service.utils.IGeneral;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/* loaded from: classes2.dex */
public final class AuthManager {
    public static final String AUTH_MOBILEID_KEY = "auth.mobileid.key";
    public static final String AUTH_TICKET_KEY = "auth.ticket.key";
    public static final String AUTH_TWFID_KEY = "auth.twfid.key";
    public static final String AUTH_URL_KEY = "auth.url.key";
    public static final String AUTH_VERSION_KEY = "auth.version.key";
    private static final String TAG = "AuthManager";
    private static final AuthManager sAuthManager = new AuthManager();
    private List<AuthResultListener> mListener = new ArrayList();
    private long mNativeListenerPtr = 0;

    /* loaded from: classes2.dex */
    public interface AuthResultListener {
        void onAuthFailed(AuthType authType, int i, String str);

        void onAuthSuccess(AuthType authType, AuthResult authResult);

        void onShouldNextAuth(AuthType authType, AuthType authType2);
    }

    private static void dispatchAuthEvent(int i) {
    }

    private native void nativeCancel();

    private native long nativeCreateGlobalListener();

    private native void nativeDestroyGlobalListener(long j);

    private native String nativeGetTwfid();

    private native boolean nativeInitAuthInfo(String str, String[] strArr, String[] strArr2);

    private native void nativeQuit();

    private native String nativeRSAEncryptWithKeyAndExponet(String str, String str2, String str3, int i);

    private native boolean nativeStartAuth(int i, String[] strArr, String[] strArr2);

    public native void nativeUpdateEFlagAndMessageDigest(String str, String str2);

    public static AuthManager getInstance() {
        return sAuthManager;
    }

    /* loaded from: classes2.dex */
    public static class AuthResult {
        public String aWorkStdVer;
        public String aWorkVer;
        public AuthType currAuth;
        public int error;
        public String notes;
        public int pwdPolicyMask;
        public int ticketEnable;
        public int ticketType;
        public String time;
        public String twfid;

        public AuthResult(int i, int i2, String str, String str2, String str3, int i3, int i4, int i5, String str4, String str5) {
            this.currAuth = AuthType.valueOf(i);
            this.error = i2;
            this.notes = str;
            this.twfid = str2;
            this.time = str3;
            this.ticketEnable = i3;
            this.ticketType = i4;
            this.pwdPolicyMask = i5;
            this.aWorkVer = str4;
            this.aWorkStdVer = str5;
        }

        public String toString() {
            return String.format(Locale.getDefault(), "\nPwdPolicy:%d \nTime:%s \nTicketEnable:%d \nTicketType:%d \naWorkVer:%s \naWorkStdVer:%s", Integer.valueOf(this.pwdPolicyMask), this.time, Integer.valueOf(this.ticketEnable), Integer.valueOf(this.ticketType), this.aWorkVer, this.aWorkStdVer);
        }
    }

    AuthManager() {
    }

    public void initAuthInfo(String str, Map<String, String> map) {
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("url cannot be empty");
        }
        if (!str.startsWith(IGeneral.PROTO_HTTP_HEAD) && !str.startsWith(IGeneral.PROTO_HTTPS_HEAD)) {
            throw new IllegalArgumentException("url must start with https:// or http://.");
        }
        if (map == null || map.isEmpty()) {
            nativeInitAuthInfo(str, null, null);
            return;
        }
        int size = map.size();
        String[] strArr = new String[size];
        String[] strArr2 = new String[size];
        int i = 0;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            strArr[i] = entry.getKey();
            strArr2[i] = entry.getValue();
            i++;
        }
        nativeInitAuthInfo(str, strArr, strArr2);
    }

    public void initAuthInfo(String str) {
        initAuthInfo(str, null);
    }

    public void startAuth(AuthType authType, Map<String, String> map) {
        if (map == null || map.size() == 0) {
            throw new IllegalArgumentException("info cannot be empty, type = " + authType);
        }
        int size = map.size();
        String[] strArr = new String[size];
        String[] strArr2 = new String[size];
        int i = 0;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            strArr[i] = entry.getKey();
            strArr2[i] = entry.getValue();
            i++;
        }
        if (nativeStartAuth(authType.intValue(), strArr, strArr2)) {
            return;
        }
        Log.error(TAG, "start auth failed, invalid argumenst, type =" + authType);
        throw new IllegalArgumentException("");
    }

    public void cancel() {
        nativeCancel();
    }

    public void quit() {
        synchronized (this.mListener) {
            this.mListener.clear();
            long j = this.mNativeListenerPtr;
            if (j != 0) {
                nativeDestroyGlobalListener(j);
                this.mNativeListenerPtr = 0L;
            }
        }
        nativeQuit();
    }

    public void registerAuthListener(AuthResultListener authResultListener) {
        synchronized (this.mListener) {
            if (this.mListener.contains(authResultListener)) {
                Log.warn(TAG, "listener:" + authResultListener.toString() + " already register, noting to do.");
            } else {
                this.mListener.add(authResultListener);
            }
            if (this.mNativeListenerPtr == 0) {
                this.mNativeListenerPtr = nativeCreateGlobalListener();
            }
        }
    }

    public void unregisterAuthListener(AuthResultListener authResultListener) {
        synchronized (this.mListener) {
            this.mListener.remove(authResultListener);
            if (this.mListener.size() == 0) {
                nativeDestroyGlobalListener(this.mNativeListenerPtr);
                this.mNativeListenerPtr = 0L;
            }
        }
    }

    protected void dispatchAuthSuccess(int i, AuthResult authResult) {
        synchronized (this.mListener) {
            for (AuthResultListener authResultListener : this.mListener) {
                if (authResultListener != null) {
                    authResultListener.onAuthSuccess(AuthType.valueOf(i), authResult);
                }
            }
        }
    }

    protected void dispatchShouldNextAuth(int i, int i2) {
        synchronized (this.mListener) {
            for (AuthResultListener authResultListener : this.mListener) {
                if (authResultListener != null) {
                    authResultListener.onShouldNextAuth(AuthType.valueOf(i), AuthType.valueOf(i2));
                }
            }
        }
    }

    protected void dispatchAuthFailed(int i, int i2, String str) {
        synchronized (this.mListener) {
            for (AuthResultListener authResultListener : this.mListener) {
                if (authResultListener != null) {
                    authResultListener.onAuthFailed(AuthType.valueOf(i), i2, str);
                }
            }
        }
    }

    public String encryptWithRSA(String str) {
        SystemConfiguration systemConfiguration = SystemConfiguration.getInstance();
        String str2 = "";
        String str3 = (String) systemConfiguration.get(2, "RSA_ENCRYPT_KEY", "");
        String str4 = (String) systemConfiguration.get(2, "RSA_ENCRYPT_EXP", "");
        if (TextUtils.isEmpty(str3)) {
            Log.info(TAG, "encrypt key is empty");
            return str;
        }
        String configEFlag = SystemConfiguration.getInstance().getConfigEFlag();
        if (TextUtils.equals(configEFlag, SystemConfiguration.CONFIG_EFLAG_CLOSE)) {
            Log.info(TAG, "flag is close");
        } else {
            str2 = configEFlag;
        }
        return nativeRSAEncryptWithKeyAndExponet(str, str2, str3, TextUtils.isEmpty(str4) ? 65537 : Integer.parseInt(str4));
    }
}
